package net.mcg_server.jbi;

import java.awt.CardLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import net.mcg_server.jbi.data.Game;
import net.mcg_server.jbi.gui.MainPanel;
import net.mcg_server.jbi.gui.menu.MenuBar;
import net.mcg_server.jbi.xml.XMLReader;

public class Program {

	// -------------
	// variables
	// -------------
	private JFrame mainFrame;
	private MainPanel mainPanel;
	private MenuBar menuBar;
	private ArrayList<Game> games = new ArrayList<Game>();
	private Properties config;
	private static Program program;

	public Program() {
		initialize();
	}

	private void initialize() {
		program = this;
		System.out.println("Application initializing.");

		config = new Properties();

		System.out.println("Loading properties file.");
		String userHomeDir = System.getProperty("user.home");

		try {
			
			File f = new File(System.getProperty("user.home")+"/Documents/JBI/jbi.properties");

			if(!f.exists()){
				createPropertiesFile(f);
			}

			InputStream inStream = new FileInputStream(userHomeDir+"/Documents/JBI/jbi.properties");
			System.out.println("Properties file loaded");
			
			System.out.println("Loading properties.");
			config.load(inStream);
		} catch (NullPointerException|IOException e) {

			System.out.println("Failed to load properties.");
			e.printStackTrace();
		}

		System.out.println("All properties loaded.");
		
		String str = config.getProperty("lastFileAccessed");
		if(str!=null){
			System.out.println("Last game list accessed: "+str);
		}

		
		// Create the main application window
		System.out.println("Creating the main Application frame.");
		mainFrame = new JFrame("Just Beat It");
		mainFrame.setBounds(200, 150, 640, 480);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.getContentPane().setLayout(new CardLayout(0,0));
		mainFrame.setResizable(false);

		// initialize and add main panel
		mainPanel = new MainPanel(this);

		// add menu bar
		menuBar = new MenuBar(mainFrame, this);

		// add text to display current JAVA version
		JLabel version = new JLabel(System.getProperty("java.verison"));
		mainPanel.add(version);

		// Load most recent game list
		System.out.println("Loading most recent game list.");
		try {
			loadRecentGameList(str);
		} catch (SAXException | IOException | ParserConfigurationException e) {
			e.printStackTrace();
		}

		// Add mainPanel to the Application frame
		mainFrame.getContentPane().add(mainPanel, "mainPanel");
		
		System.out.println("Application loaded.");
		
	}

	public MainPanel getMainPanel(){
		return mainPanel;
	}

	public MenuBar getMenuBar(){
		return menuBar;
	}

	public ArrayList<Game> getGameList(){
		return games;
	}

	public JFrame getMainFrame(){
		return mainFrame;
	}

	public void clearGameList() {
		games.clear();
		System.out.println("Game List cleared");
	}

	public void writeLastFileProperty(String location){
		System.out.println("Saving LastFileAccessed location: " + location);
		config.setProperty("lastFileAccessed", location);
	}

	public void saveProperties(){

		try {
			config.store(new FileOutputStream(System.getProperty("user.home")+"/Documents/JBI/jbi.properties"), null);
			System.out.println("Properties saved");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	private static void createPropertiesFile(File f){
		System.out.println("File not found.  Creating Property file");
		File d = new File(System.getProperty("user.home")+"/Documents/JBI/GameLists");
		d.mkdirs();
		try {
			f.createNewFile();
		} catch (IOException e) {
			System.out.println("Failed to create property file.");
			e.printStackTrace();
		}
		program.writeLastFileProperty("");

		program.saveProperties();
	}
	
	private static void loadRecentGameList(String loc) throws SAXException, IOException, ParserConfigurationException{
		XMLReader read = new XMLReader();
		read.readXml(program, loc);
	}

}
